/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.sgf.compile;

import com.inspur.edp.sgf.api.entity.ServiceOperation;
import com.inspur.edp.sgf.api.entity.SgMetadata;
import org.apache.commons.lang3.StringUtils;

import java.text.MessageFormat;
import java.util.ArrayList;

/**
 * 功能描述: @ClassName: EapiExtension @Author: Fynn Qi @Date: 2020/9/17 14:10 @Version: V1.0
 */
public class EapiExtension {

    public static boolean hasBaseClass(SgMetadata eapi) {
        switch (getResourceType(eapi)) {
            case ResourceType.EAPI_RESOURCE_TYPE_VO:
            case ResourceType.EAPI_RESOURCE_TYPE_VO_SIMPLE:
                return true;
            default:
                return false;
        }
    }

    public static boolean hasConstructorImpl(SgMetadata eapi) {
        switch (getResourceType(eapi)) {
            case ResourceType.EAPI_RESOURCE_TYPE_VO:
            case ResourceType.EAPI_RESOURCE_TYPE_VO_SIMPLE:
                return true;
            default:
                return false;
        }
    }

    public static String getBaseClass(SgMetadata eapi) {
        StringBuilder sb = new StringBuilder();
        switch (getResourceType(eapi)) {
            case ResourceType.EAPI_RESOURCE_TYPE_VO:
                sb.append("com.inspur.edp.sgf.base.VoBaseServiceImpl");
                break;
            case ResourceType.EAPI_RESOURCE_TYPE_VO_SIMPLE:
                sb.append("com.inspur.edp.sgf.base.simple.SimpleVoBaseServiceImpl");
                break;
            default:
                break;
        }
        return sb.toString();
    }

    public static String getConstructor(SgMetadata eapi, String className) {
        StringBuilder sb = new StringBuilder();
        sb.append(MessageFormat.format("  public {0}()'{'", className)).append(System.lineSeparator());
        switch (getResourceType(eapi)) {
            case ResourceType.EAPI_RESOURCE_TYPE_VO:
            case ResourceType.EAPI_RESOURCE_TYPE_VO_SIMPLE:
                sb.append(getViewModelConstructorExtension(eapi, className));
                break;
            default:
                break;
        }
        sb.append(getGlobalContext(eapi));
        sb.append("  }").append(System.lineSeparator());
        return sb.toString();
    }

    private static String getViewModelConstructorExtension(SgMetadata metadata, String className) {
        StringBuilder sb = new StringBuilder();
        sb.append(MessageFormat.format("    this.setEnableGlobalTransactionMethods({0});", getEnaEnableGlobalTransactionMethodCodes(metadata))).append(System.lineSeparator());
        return sb.toString();
    }

    private static String getEnaEnableGlobalTransactionMethodCodes(SgMetadata metadata) {
        StringBuilder sb = new StringBuilder();
        if (metadata == null || metadata.getService() == null || metadata.getService().getOperations() == null || metadata.getService().getOperations().size() <= 0) {
            return sb.toString();
        }
        ArrayList<ServiceOperation> operations = metadata.getService().getOperations();
        int count = 0;
        for (ServiceOperation operation : operations) {
            if (operation.isTransaction()) {
                if (count > 0) {
                    sb.append(",");
                }
                sb.append(String.format("\"%s\"", operation.getCode()));
                count++;
            }
        }
        return sb.toString();
    }

    private static String getGlobalContext(SgMetadata metadata) {
        StringBuilder sb = new StringBuilder();
        sb.append(MessageFormat.format("    this.getGlobalContext().put(\"{0}\", \"{1}\");", "externalApiMetadataId", metadata.getId())).append(System.lineSeparator());
        //修改日期：2021-7-6
        //修改说明: 增加资源信息上下文
        sb.append(MessageFormat.format("    this.getGlobalContext().put(\"{0}\", \"{1}\");", "resourceId", metadata.getService().getResourceId())).append(System.lineSeparator());
        sb.append(MessageFormat.format("    this.getGlobalContext().put(\"{0}\", \"{1}\");", "resourceType", metadata.getService().getResourceType())).append(System.lineSeparator());
        if (metadata.getService().getExtension() != null) {
            for (String key : metadata.getService().getExtension().keySet()) {
                sb.append(MessageFormat.format("    this.getGlobalContext().put(\"{0}\", \"{1}\");", key, metadata.getService().getExtension().get(key))).append(System.lineSeparator());
            }
        }
        return sb.toString();
    }

    private static String getResourceType(SgMetadata eapi) {
        String resourceType = eapi.getService().getResourceType();
        if (StringUtils.isBlank(resourceType)) {
            throw new RuntimeException(String.format("外部服务元数据【%s】(ID:%s)的资源类型为空，无法生成外部服务代理类代码", eapi.getCode(), eapi.getClass()));
        }
        return resourceType;
    }
}
